﻿/**
* @projectName   cvvision
* @file          IFlowTreeManager.h
* @brief         流程树管理类
* @author        GuoFeng
* @contact        805305774@qq.com/feng.guo@micro-i.com.cn
* @date          2022-04-28
*/

#ifndef IFLOW_TREE_MANAGER_H
#define IFLOW_TREE_MANAGER_H

#include <QObject>
#include <QHash>
#include <QJsonArray>
#include <QDomElement>


#include "Interface_Global.h"
#include "FlowTree.h"
#include "FlowCellParam.h"

namespace CVV
{
namespace Interface
{

class INTERFACE_EXPORT IFlowTreeManager : public QObject
{
    Q_OBJECT

public:
    static IFlowTreeManager* instance();

    /**
     * @brief updateFlowTree 更新流程树
     * @param flowId
     * @param hashFlowTree
     */
    void updateFlowTree(const QString &flowId, const QMap<QString, FlowTree> &hashFlowTree);

    /**
     * @brief initPluginParams 初始化插件参数
     * @param flowId
     * @param pluginParam
     */
    void initPluginParams(const QString &flowId, const QHash<QString, QJsonArray>& pluginParam);

    /**
     * @brief updateDynamicParam 更新动态参数
     * @param flowId
     * @param cellId
     * @param pluginCode
     * @param dynamicParam 动态属性参数
     */
    void updateDynamicParam(const QString &flowId, const QString &cellId, const QString &pluginCode, const QJsonArray &dynamicParam);

    /**
     * @brief getPrevOutFlowTree 获取当前单元之前流程节点
     * @param flowId
     * @param cellId
     * @return
     */
    QList<FlowTree> getPrevOutFlowTree(const QString &flowId, const QString &cellId);

    /**
     * @brief getFlowTree 指定流程的流程树
     * @param flowId
     * @return
     */
    QHash<QString, FlowTree> getFlowTree(const QString &flowId);

    /**
     * @brief getPluginParam 获取指定流程的指定节点的参数
     * @param flowId
     * @param cellId
     * @return
     */
    QJsonArray getPluginParam(const QString &flowId, const QString &cellId, const QString &pluginCode);

    /**
     * @brief clearFlowTree 清楚流程树参数和树节点
     * @param flowId
     */
    void clearFlowTree(const QString &flowId);

    /**
     * @brief getFlowCellParamByFile 获取流程树列表<cellId, >
     * @param fileName 流程文件名
     * @return 流程树节点列表参数
     */
    QHash<QString, QList<FlowCellParam>> getFlowCellParamByFile(const QString &fileName);

signals:

private:
    explicit IFlowTreeManager(QObject *parent = nullptr);
    virtual ~IFlowTreeManager();

    /**
     * @brief findAllPreOutFlowTree 查找所有的前节点
     * @param lstPreFlowTree
     */
    void findAllPreOutFlowTree(const QList<FlowTree> &lstAllFlowTree,
                               const FlowTree &paFlowTree,
                               QList<FlowTree> &lstPreFlowTree);

    /**
     * @brief findChildPlugin 找子插件
     * @param element
     * @param level 层级
     * @param mapflowTree
     */
    void findChildPlugin(const QDomElement &element,
                         int &level,
                         QMap<QString, FlowTree>& mapflowTree);

private:
    QHash<QString, QHash<QString, FlowTree>> m_hashFlowTree;        // 流程树<flow id, <cell id, flowtree>>
    QHash<QString, QHash<QString, QJsonArray>> m_hashPluginParams;  // 流程树参数 <flow id, <plugincode, plugin param>>

};

}
}

#ifndef gFlowTreeManager
#define gFlowTreeManager CVV::Interface::IFlowTreeManager::instance()
#endif

#endif // IFLOWTREEMANAGER_H
